package com.gking.processHarvest.controller;

import com.gking.processHarvest.common.BaseContext;
import com.gking.processHarvest.common.Res;
import com.gking.processHarvest.dto.ModifyPasswordDto;
import com.gking.processHarvest.entity.Master;
import com.gking.processHarvest.entity.Uploads;
import com.gking.processHarvest.service.Master.MasterService;
import com.gking.processHarvest.service.Uploads.UploadsService;
import com.gking.processHarvest.utils.UploadFile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


@Slf4j
@RestController
public class MasterController {

    @Autowired
    private MasterService masterService;

    @Autowired
    private UploadsService uploadsService;

    /**
     * 注册用户
     *
     * @param master 用户信息
     * @return s
     */
    @PostMapping("/api/register")
    public Res<String> regUser(@RequestBody Master master) {
        log.warn("注册信息 master 为: {}", master);
        String account = master.getAccount().trim();
        String password = master.getPassword().trim();
        if (account.equals("") || password.equals("")) return Res.error("请将参数补充完整！");
        return masterService.registerUser(account, password);
    }

    /**
     * 校验账号
     *
     * @param request 线程
     * @param master  用户信息
     * @return s
     */
    @PostMapping("/api/verify")
    public Res<Master> verifyAccount(HttpServletRequest request, @RequestBody Master master) {
        log.warn("校验用户登录的账号 account 为 : {}", master);
        return masterService.verifyAccount(request, master.getAccount());
    }

    /**
     * 登录
     *
     * @param request 线程
     * @param master  用户信息
     * @return s
     */
    @PostMapping("/api/login")
    public Res<String> login(HttpServletRequest request, @RequestBody Master master) {
        log.warn("用户登录信息为：{}", master);
        return masterService.loginUser(request, master.getPassword());
    }

    /**
     * 退出登录
     *
     * @return s
     */
    @PostMapping("/user/logout")
    public Res<String> logout() {
        return Res.success("退出成功！");
    }

    /**
     * 验证登录状态
     *
     * @return s
     */
    @PostMapping("/user/status")
    public Res<Boolean> getLoginStatus() {
        if (BaseContext.getCurrentId() == null) return Res.success("验证身份成功", false);
        else return Res.success("验证身份成功", true);
    }

    /**
     * 获取用户信息
     *
     * @return s
     */
    @GetMapping("/user/info")
    public Res<Master> getUserInfo() {
        return masterService.getUserInfo();
    }

    /**
     * 更新信息
     *
     * @param master 用户信息
     * @return s
     */
    @PostMapping("/user/updateInfo")
    public Res<String> updateInfo(@RequestBody Master master) {
        log.warn("更新的用户信息为：{}", master);
        return masterService.updateInfo(master.getNickname(), master.getDescription(), master.getEmail());
    }


    /**
     * 修改密码
     *
     * @param modifyPasswordDto 密码信息
     * @return s
     */
    @PutMapping("/user/modifyPassword")
    public Res<String> modifyPassword(@RequestBody ModifyPasswordDto modifyPasswordDto) {
        log.warn("修改的用户密码为：{}", modifyPasswordDto);
        return masterService.modifyPassword(modifyPasswordDto.getOldPassword(), modifyPasswordDto.getNewPassword());
    }


    /**
     * 上传头像
     *
     * @param file 头像文件
     * @return s
     */
    @PostMapping("/user/avatar")
    public Res<String> updateAvatar(@RequestBody Uploads file) {
        if (file == null) return Res.error("头像文件不能为空");
        if (!"image/jpeg".equals(file.getType())) return Res.error("上传的文件必须为图片格式！");
        Integer i = new Integer(file.getSize());
        if (i > 1 * 1024 * 1024) return Res.error("上传的文件必须小于500KB");

        return masterService.updataAvatar(file);
    }
//    public Res<String> updateAvatar(@RequestParam("avatar") MultipartFile file) {
//        if (file.isEmpty()) return Res.error("头像文件不能为空");
//        if (!"image/jpeg".equals(file.getContentType())) return Res.error("上传的文件必须为图片格式！");
//        if (file.getSize() > 1 * 500 * 1024) return Res.error("上传的文件必须小于500KB");
//
//        return masterService.updataAvatar(file);
//    }


    /**
     * 下载头像文件
     *
     * @param path 图片地址
     * @return s
     */
    @GetMapping("/api/download")
    public Res<String> downloadFile(String path) {
        log.warn("path 的值为：{}", path);
        if (path.equals("")) return Res.error("请将参数补充完整");
        return masterService.downloadFile(path);
    }
}
